package com.dayaokeji.rhythmschoolstudent.boxing;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.config.BoxingConfig;
import com.bilibili.boxing.model.config.BoxingCropOption;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;
import com.bilibili.boxing.utils.BoxingFileHelper;
import com.bilibili.boxing_impl.ui.BoxingActivity;
import com.dayaokeji.rhythmschoolstudent.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by Zeno on 2017/3/22.
 *
 * 图片选择器帮助类
 */

public class BoxingUtil {


    public static void startSingleChooseImage(Activity activity,int requestCode) {
        if (!activity.isFinishing()) {
            BoxingConfig boxingConfig = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG);
            boxingConfig.needPaging(false).needCamera(R.mipmap.ic_boxing_camera_white);
            Boxing.of(boxingConfig).withIntent(activity, BoxingActivity.class).start(activity,requestCode);
        }
    }

    public static void startSingleChooseImageCrop(Activity activity,int requestCode) {
        if (!activity.isFinishing()) {
            String cachePath = BoxingFileHelper.getCacheDir(activity);
            if (TextUtils.isEmpty(cachePath)) {
                return;
            }
            Uri destUri = new Uri.Builder()
                    .scheme("file")
                    .appendPath(cachePath)
                    .appendPath(String.format(Locale.CHINA, "%s.png", System.currentTimeMillis()))
                    .build();
            BoxingConfig boxingConfig = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG).withCropOption(new BoxingCropOption(destUri));
            boxingConfig.needPaging(false).withMaxCount(1).needCamera(R.mipmap.ic_boxing_camera_white);
            Boxing.of(boxingConfig).withIntent(activity, BoxingActivity.class).start(activity,requestCode);
        }
    }

    /**
     * 打开图片多选
     * @param activity
     */
    public static void startMultiChooseImage(Activity activity,int requestCode,int maxCount) {
        if (!activity.isFinishing()) {
            BoxingConfig boxingConfig = new BoxingConfig(BoxingConfig.Mode.MULTI_IMG);
            boxingConfig.needGif().needPaging(true).withMaxCount(maxCount);
            Boxing.of(boxingConfig).withIntent(activity, BoxingActivity.class).start(activity,requestCode);
        }
    }

    /**
     * 打开图片多选
     * @param fragment
     */
    public static void startMultiChooseImage(Fragment fragment, int requestCode, int maxCount) {
        if (!fragment.isDetached()) {
            BoxingConfig boxingConfig = new BoxingConfig(BoxingConfig.Mode.MULTI_IMG);
            boxingConfig.needGif().needPaging(true).withMaxCount(maxCount);
            Boxing.of(boxingConfig).withIntent(fragment.getContext(), BoxingActivity.class).start(fragment,requestCode);
        }
    }

    /**
     * 得到选择图片的路径
     * @param data
     * @return
     */
    public static String getResultFilePath(Intent data) {
        if (data != null) {
            final ArrayList<BaseMedia> medias = Boxing.getResult(data);
            BaseMedia baseMedia = medias.get(0);
            if (!(baseMedia instanceof ImageMedia)) {
                return null;
            }
            return ((ImageMedia) baseMedia).getThumbnailPath();
        }
        return "";

    }

    /**
     * 得到多选图片的BaseMedia
     * @param data
     * @return
     */
    public static List<BaseMedia> getResultFileList(Intent data) {
        return Boxing.getResult(data);
    }
}
